<template>
  <el-container class="main">
    <el-header>
      <div class="title">
        <img src="@/assets/logo.png" alt="" height="100%" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div @click="toggleCollapse">
          <i
            :class="isCollapse ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"
          ></i>
        </div>
        <el-menu
          :default-active="this.$route.path"
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          background-color="#333744"
          active-text-color="#409eff"
          text-color="#fff"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menuList: [],
      iconList: {
        125: "el-icon-user",
        103: "el-icon-set-up",
        101: "el-icon-goods",
        102: "el-icon-tickets",
        145: "el-icon-data-line",
      },
      isCollapse: false,
    };
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
      this.$message.success("退出成功");
    },
    async getMenuList() {
      const { data: res } = await this.$http("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      // console.log(this.menuList);
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
  },
  created() {
    this.getMenuList();
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .title {
    height: 100%;
    display: flex;
    align-items: center;

    span {
      color: #fff;
      font-size: 20px;
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  color: #fff;
  div {
    line-height: 34px;
    text-align: center;
  }
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.main {
  height: 100%;
  .el-container{
    overflow: hidden;
  }
}
</style>
